package com.wesly.ssm.web;

import com.wesly.ssm.entity.User;
import com.wesly.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping("/user")
public class UserController {

  @Autowired
  private UserService userService;

  @RequestMapping("/")
  public String def() {
    return "defalut";
  }

  @RequestMapping("/test")
  public String test() {
    return "list";
  }

  @RequestMapping("/index")
  public String index() {
    return "login";
  }

  @RequestMapping("/login")
  @ResponseBody
  public Object login(User user, HttpServletRequest request) {
    User loginUser = userService.login(user);
    if (loginUser != null) {
      request.getSession().setAttribute("currentUser",loginUser);
      return user;
    } else {
      return "{\"returncode\":0,\"message\":\"hello world\"}";
    }
  }

  @RequestMapping("/search")
  public String search() {
    return "login";
  }
}
